package com.clementheliou.cheliou.dal.model.member.enumeration;

import java.util.Locale;

/**
 * Locale used by a member with an untranslatable label. It's composed of a
 * classic {@link Locale} with a country and a language. The first one is an
 * ISO-3166 code while the second one is an ISO-639-1 code. More details can be
 * found on the following links:
 * <ul>
 * <li><i>http://www.iso.org/iso/fr/home/standards/country_codes/
 * country_names_and_code_elements.htm;</i></li>
 * <li><i>http://fr.wikipedia.org/wiki/Liste_des_codes_ISO_639-1.</i></li>
 * </ul>
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @since 1.0
 */
public enum MemberLocale {

	/**
	 * English locale for United Kingdom.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	ENGLISH(Locale.UK, "English (GB)"),

	/**
	 * French locale for France.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	FRANCAIS(Locale.FRANCE, "Français");

	/**
	 * The locale.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private Locale locale;

	/**
	 * The unlocalized locale label.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	private String label;

	/**
	 * Constructor from the given locale and label.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param locale the locale to be used.
	 * @param label the unlocalized label to be used.
	 * @since 1.0
	 */
	private MemberLocale(final Locale locale, final String label) {
		this.locale = locale;
		this.label = label;
	}

	/**
	 * Checks if the given locale can be used in the application. The comparison
	 * is made with the locale's languages.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param locale the locale to be checked.
	 * @return the checking result.
	 * @since 1.0
	 */
	public static boolean isLocaleAccepted(final Locale locale) {
		for (MemberLocale memberLocale : MemberLocale.values()) {
			if (memberLocale.getLocale().equals(locale)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Gets the {@link MemberLocale} related to the given {@link Locale}.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @param locale the locale that the member locale must include.
	 * @return the related {@link MemberLocale} if exists, <tt>null</tt>
	 *         otherwise.
	 * @since 1.0
	 */
	public static MemberLocale getMemberLocaleFromLocale(final Locale locale) {
		for (MemberLocale memberLocale : MemberLocale.values()) {
			if (memberLocale.getLocale().equals(locale)) {
				return memberLocale;
			}
		}
		return null;
	}

	/*
	 * Getters & setters.
	 */

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}
}
